package com.meme.editor.model.sprite2d;

import java.awt.Graphics2D;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.meme.editor.EntityObject;
import com.meme.editor.event.EntityChangedEvent;


public class AnimatedModule extends CompositedModule //Module
{
	static Logger logger = Logger.getLogger (AnimatedModule.class);
	
	public AnimatedModule ()
	{
		this (null);
	}
	
	public AnimatedModule (String name)
	{
		super (name);
		
		// build property manager
	}
	
	public ModuleSequence createNewModuleSequence ()
	{
		addNewChild ();
		
		return (ModuleSequence) getChildByIndex (getChildrenCount () - 1);
	}
	
	@Override
	public String getDefaultName ()
	{
		return "Animated";
	}
	
	@Override
	protected boolean isValidChild (EntityObject entity)
	{
		return entity instanceof ModuleSequence;
	}
	
	@Override
	public EntityObject insertNewChild (int insertPos)
	{
		ModuleSequence instance = new ModuleSequence ();
		
		return insertChild (instance, insertPos);
		
		// temp, should fire child appended event
		//fireEntityChangedEvent();
		
		
	}
	
	@Override
	public int getMinX ()
	{
		int minX = Integer.MAX_VALUE;
		
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			int x = ( (ModulePart) getChildByIndex (i) ).getMinX();
			if ( x < minX )
				minX = x;
		}
		
		return minX;
	}
	
//	@Override
//	public int getMaxX ()
//	{
//		int maxX = Integer.MIN_VALUE;
//		
//		for ( int i = 0; i < getChildrenCount(); ++ i )
//		{
//			int x = ( (ModuleInstance) getChildByIndex (i) ).getMaxX();
//			if ( x > maxX )
//				maxX = x;
//		}
//		
//		return maxX;
//	}
	
	@Override
	public int getMinY ()
	{
		int minY = Integer.MAX_VALUE;
		
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			int y = ( (ModulePart) getChildByIndex (i) ).getMinY();
			if ( y < minY )
				minY = y;
		}
		
		return minY;
	}
	
//	@Override
//	public int getMaxY ()
//	{
//		int maxY = Integer.MIN_VALUE;
//		
//		for ( int i = 0; i < getChildrenCount(); ++ i )
//		{
//			int y = ( (ModuleInstance) getChildByIndex (i) ).getMaxY();
//			if ( y > maxY )
//				maxY = y;
//		}
//		
//		return maxY;
//	}
	
	@Override
	public int getWidth ()
	{
		if ( getChildrenCount () == 0 )
			return 0;
		
		int minX = Integer.MAX_VALUE;
		int maxX = Integer.MIN_VALUE;
		
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			int x = ( (ModulePart) getChildByIndex (i) ).getMinX();
			if ( x < minX )
				minX = x;
			
			x += ( (ModulePart) getChildByIndex (i) ).getWidth();
			if ( x > maxX )
				maxX = x;
		}
		
		return maxX - minX;
	}
	
	@Override
	public int getHeight ()
	{
		if ( getChildrenCount () == 0 )
			return 0;
		
		int minY = Integer.MAX_VALUE;
		int maxY = Integer.MIN_VALUE;
		
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			int y = ( (ModulePart) getChildByIndex (i) ).getMinY();
			if ( y < minY )
				minY = y;
			
			y += ( (ModulePart) getChildByIndex (i) ).getHeight();
			if ( y > maxY )
				maxY = y;
		}
		
		return maxY - minY;
	}
	
	@Override
	public boolean isImageHybird ()
	{
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			ModuleSequence mi = (ModuleSequence) getChildByIndex (i);
			if ( mi.isImageHybird () )
				return true;
		}
		
		return false;
	}

	@Override
	public boolean isPaletteHybird ()
	{
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			ModuleSequence mi = (ModuleSequence) getChildByIndex (i);
			if ( mi.isImageHybird () || mi.isPaletteHybird () )
				return true;
		}
		
		return false;
	}
	
	@Override
	public void render (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flags)
	{
		for ( int i = 0; i < getChildrenCount (); ++ i)
		{
			ModulePart mi = (ModulePart) getChildByIndex (i);
			mi.render(g, posX, posY, flipX, flipY, scale, flags);
			
			// only the first frame is rendered
			break;
		}
	}
	
	// special for AnimatedModule
	public void render (Graphics2D g, int posX, int posY, int scale, int frameID)
	{
		logger.assertLog (0 <= frameID && frameID < getChildrenCount (), "out of range");
		
		if ( 0 <= frameID && frameID < getChildrenCount () )
		{
			ModulePart mi = (ModulePart) getChildByIndex (frameID);
			mi.render(g, posX, posY, false, false, scale, 0);
		}
	}
	
	public boolean isChildAtPos (int frameID, int x, int y)
	{
		if ( 0 <= frameID && frameID < getChildrenCount () )
		{
			ModulePart mi = (ModulePart) getChildByIndex (frameID);
			int left = mi.getMinX();
			int right = left + mi.getWidth();
			int top = mi.getMinY();
			int bottom = top + mi.getHeight();
			if ( left <= x && x < right && top <= y && y < bottom )
				return true;
		}
		
		return false;
	}
}
